package com.consente.demo.main;

import com.consente.demo.bean.SpringContextUtils;
import com.consente.demo.crc.Crc;
import com.consente.demo.entity.Data;
import com.consente.demo.netty.service.NettyServerHandlerTemplate;
import com.consente.demo.netty.service.NettyServerStarterTemplate;
import com.consente.demo.service.DataService;
import io.netty.channel.ChannelHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.xml.bind.DatatypeConverter;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.Date;

public class Main {

    private static final Logger log = LoggerFactory.getLogger(Main.class);
    public String code(String str){
//        String str = "01 03 0c 00 01 00 02 00 02 00 44 00 AA 00 55 BD 3C";
//        String str = "04 03 0c 00 00 00 00 00 16 00 92 00 aa 00 55 78 70";
        
        String substring = str.substring(1, 2);
        String split = "(.{2})";
        String newStr = str.replaceAll(split, "$1 ");
        log.info("将无空格字符串用空格两个一组间隔开为：" + newStr);
        //空格分割
        String[] s = newStr.split(" ");
        //获取第三个元素(数据位数)
        String s1 = s[2];
        //16进制转10
        int i = Integer.parseInt(s1, 16);
        //截取数据
        String[] ss = Arrays.copyOfRange(s, 3, i - 1);
        //拼成字符串
        String ll = "";
        //末尾截取校验位
        String ff = s[s.length - 2];
        String bb = s[s.length - 1];
        String s2 = ff.toUpperCase() + bb.toUpperCase();
        String substring1 = str.substring(0, str.length() - 4);
        String crc3 = Crc.getCRC3(DatatypeConverter.parseHexBinary(substring1));
        log.info("解析完协议为:"+str);
        log.info("校验位为："+s2);
        log.info("实际校验位为："+crc3);
        if(!crc3.equals(s2)){
        return "error";
        }
        //遍历数据位
        for (int j = 0; j < ss.length; j++) {
            String s3 = ss[j];
            //如果第六位就结束拼接
            if (j >= 6) break;
            //第0、2、4位是00不用拼接
            if (j == 0 || j == 2 || j == 4) continue;
            //倒数第二位如果是00替换小数点
            if (j == ss.length - 2) {
                String replace = s3.replace(s3, ".");
                ll += replace;
                continue;
            }
            ll += s3;
        }
        //转int
        int i1 = Integer.parseInt(ll);
        //拼小数点与数据最后一位
        String s3 = i1 + "." + ss[ss.length - 1];
        DataService dataService = (DataService) SpringContextUtils.getBean("DataService");
        Data data = new Data();
        data.setCode("A"+substring);
        data.setValue(s3);
        data.setTime(new Date());
        Data byCode = dataService.selectDataByCode("A"+substring);
        if (byCode == null) {
            int insert = dataService.insert(data);
            if(insert ==1 ) log.info("添加成功");
            else {
                log.info("添加失败");
            }
        } else {
            if(s3.equals(byCode.getValue())){
                log.info("数据相同不做修改");
                return s3;
            }
            int update = dataService.update(data);
            if(update ==1 ) log.info("修改成功");
            else {
                log.info("修改失败");
            }
        }
        return s3;
    }


}
